<?php
namespace application\cms\controller\admin;


class Slider{

    //轮播图首页
    function index(){   

        //查询轮播图列表
        $sql="select * from ".tablename('rj_cms_slider')." where type='轮播' ";
        if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' ){
            $keyword = trim($_REQUEST['keyword']);
            $sql .= " and name like '%$keyword%' ";
        }        
        $sql.= " order by list_order";

        //$page=new \library\deep\Page(20);
        $page= \library\deep\Page::new_instance();

        $sql=$page->getPageSQL( $sql , 20 );

        $rs=$GLOBALS['db']->query($sql);
        
        require view('admin/slider/slider.tpl.php');
    } 

    //添加轮播图页面
    function slider_add(){
        require view('admin/slider/slider_add.tpl.php');
    }

    //修改轮播图页面
    function slider_edit(){
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="select * from ".tablename('rj_cms_slider')." where id=".$id;
            $ret=$GLOBALS['db']->query($sql)->fetch();
        }else{
            message('未找到该轮播图！',url([ _MODULE_NAME_ ,'admin/slider','index']));
        }
        require view('admin/slider/slider_edit.tpl.php');
    }

    //保存
    function slider_save(){
        if(!isset($_POST['name']) && $_POST['name']==''){
            message('轮播图名称不能为空！',url([ _MODULE_NAME_ ,'admin/slider','index']));
        }

        $data = [
            'name'=> $_POST['name'],
            'slider_img'=> isset($_POST['slider_img'])?$_POST['slider_img']:'',
            'tip'=>$_POST['tip'],
            'url'=>$_POST['url'],
            'list_order'=>$_POST['list_order'],
            'status'=>$_POST['status'],
        ];

        if( isset($_POST['id']) && $_POST['id']!=''){
            //修改
            //条件
            $where['id']= intval($_POST['id']);

            //执行sql
            $ok = pdo_update('rj_cms_slider',$data,$where);
            if($ok){
                message('修改成功！',url([ _MODULE_NAME_ ,'admin/slider','index']));
            }else{
                message('修改失败！',url([ _MODULE_NAME_ ,'admin/slider','index']));
            }
        }else{
            //添加
            //执行sql
            $ok = pdo_insert('rj_cms_slider',$data);
            if($ok){
                message('添加成功！',url([ _MODULE_NAME_ ,'admin/slider','index']));
            }else{
                message('添加失败！',url([ _MODULE_NAME_ ,'admin/slider','index']));
            }
        }
    }
    
    //删除轮播图
    function slider_delete(){
        
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="delete from ".tablename('rj_cms_slider')." where id=".$id;
            $ok=$GLOBALS['db']->exec($sql);
            if($ok){
                message('删除成功',url([ _MODULE_NAME_ ,'admin/slider','index']));
            }else{
                message('删除失败',url([ _MODULE_NAME_ ,'admin/slider','index']));
            }
        }else{
            message('未找到该轮播图,请刷新页面',url([ _MODULE_NAME_ ,'admin/slider','index']));
        }
    }

}
